<template>
  <el-dialog title="添加菜单" :visible.sync="dialogFormVisible">
    <el-form :model="form" name="el-form-sys-role-edit-name" id="el-form-sys-role-edit-id"  size="mini">
      <el-form-item label="菜单名称" :label-width="formLabelWidth">
        <el-input v-model="form.name" name="roleName" autocomplete="off"></el-input>
      </el-form-item>


      <el-form-item label="上级菜单" :label-width="formLabelWidth">
        <el-select v-model="parentName" placeholder="上级菜单" style="width: 100%" @change="_pNameChange">
          <el-option v-for="item in pNameList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="编码" :label-width="formLabelWidth">
        <el-input v-model="form.code" name="code" autocomplete="off"></el-input>
      </el-form-item>


      <el-form-item label="URL" :label-width="formLabelWidth">
        <el-input v-model="form.url" name="url" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="图标编码" :label-width="formLabelWidth">
        <el-input v-model="form.pic" name="pic" autocomplete="off"></el-input>
      </el-form-item>


      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="form.remark" name="remark" autocomplete="off"></el-input>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="_ok">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

    export default {

      mounted(){
        this._listParent();
      },

        name: "SysMenuEdit",
      data(){
          return{

            formAdd:{
              name:'',
              parentName:'',
              code:'',
              url:'',
              pic:'',
              remark:'',
              pid:''
            },
            form:{
              name:'',
              parentName:'',
              code:'',
              url:'',
              pic:'',
              remark:'',
              pid:''
            },

            pNameList:[],
            parentName: '',

            dialogFormVisible: false,

            formLabelWidth: '120px'
          }
      },
      methods:{

          _open(obj){

            if (undefined===obj){
              this.parentName='';
              this.form= JSON.parse(JSON.stringify(this.formAdd));;
              console.log('-----formAdd------',this.formAdd)
              this.dialogFormVisible=true;
            }else{
              this.form=obj;
              this.parentName=obj.parentName;
              this.dialogFormVisible=true;
            }

          },

        _pNameChange(data){
            this.form.pid=data;
        },

        _listParent(){
          this.$http.get('/sys_resource/list_parent').then(res=>{
                this.pNameList=res.data.data;
          }).catch(err=>{

          });
        },


          _ok(){
            let userData= this.form;
            if (undefined === userData.id){
              this.$http.post('/sys_resource/add',userData).then(res=>{
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
                this.$emit('on-result-change')
                this.dialogFormVisible = false;

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '添加失败'
                });
              })

            } else {
              this.$http.put('/sys_resource/'+userData.id,userData).then(res=>{
                this.$message({
                  type: 'success',
                  message: '编辑成功!'
                });
                this.$emit('on-result-change')
                this.dialogFormVisible = false;

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '编辑失败'
                });
              })
            }




          }

      },
    }
</script>

<style scoped>

</style>
